/* SQUIB - Scala's Quirky User Interface Builder 
    Copyright (C) 2008 Tim Dalton

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 
*/

import java.awt._
import java.awt.GridBagConstraints._
import java.beans.{PropertyChangeListener, PropertyChangeEvent}
import java.lang.reflect.Method
import javax.swing._
import javax.swing.event._

import scala.reflect.BeanProperty

import net.miginfocom.swing.MigLayout

package tfd.scala.squib.demo {

import tfd.scala.squib._
import tfd.scala.squib.event._

 object RGBSliders extends Application {
    class RGBCanvasComponent extends JComponent {
        private var red:Int = 0
        private var green:Int = 0
        private var blue:Int = 0
        
        def setRed(value:Int) = {
            if (value != red) {
                red = value
                repaint()
            }
        }
        
        def setGreen(value:Int) = {
            if (value != green) {
                green = value
                repaint()
            }
        }
        
        def setBlue(value:Int) = {
            if (value != blue) {
                blue = value
                repaint()
            }
        }
           
        override def paint(g: Graphics):Unit = {
            g.setColor(new Color(red, green, blue))
            val d = getSize()
            g.fillRect(0,0, d.width, d.height)
        }
    }
    
    object rgbcanvas extends BuiltComponent[RGBCanvasComponent] {
        def newComponent = new RGBCanvasComponent()
    }
    
    lazy val rgb = rgbcanvas.id("rgbcanvas")
    
    frame(
            'title->"RGB Selector",
            'visible -> true,
            'defaultCloseOperation -> JFrame.EXIT_ON_CLOSE,
            'layout -> new MigLayout("fill, wrap 3", "align right, grow", "align top, grow"),
            contents(
                 label("Red:"),
                 slider("red", 'maximum -> 255, 'minimum-> 0, 'value->0, sliderValueChanged(&rgb.setRed)),
                 rgbcanvas("rgbcanvas",
                               'preferredSize -> new Dimension(100,100),
                               'red -> 0,
                               'green -> 0,
                               'blue -> 0
                 ) -> "spany 3",
                 label("Green:"),
                 slider("green", 'maximum -> 255, 'minimum-> 0, 'value->0, sliderValueChanged(&rgb.setGreen)),
                 label("Blue:"),
                 slider("blue", 'maximum -> 255, 'minimum-> 0, 'value->0, sliderValueChanged(&rgb.setBlue))                
            )
    ).pack
 }
}